package com.diana.config;

import com.diana.common.Utils;
import com.diana.filter.MainFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import com.alibaba.fastjson.JSONException;

@RestControllerAdvice
public class ExceptionConfig {
	private static final Logger LOG = LoggerFactory.getLogger(MainFilter.class);

	@ExceptionHandler(value = JSONException.class)
	public String JSONExceptionHandler(JSONException e) {
		LOG.error("-------JSONException--------",e);
		return Utils.message("1101", "JSON解析异常", e.getMessage());
	}

	@ExceptionHandler(value = MultipartException.class)
	public String FileSizeLimitExceededExceptionHandler(MultipartException e) {
		LOG.error("-------MultipartException--------",e);
		return Utils.message("1102", "上传文件大小或格式有误", e.getMessage());
	}


	@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
	public String MethodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException e) {
		LOG.error("-------MethodArgumentTypeMismatchException--------",e);
		return Utils.message("1103", "参数只能是整数", e.getMessage());
	}

	@ExceptionHandler(value = ServletRequestBindingException.class)
	public String ServletRequestBindingExceptionHandler(ServletRequestBindingException e) {
		LOG.error("-------ServletRequestBindingException--------",e);
		return Utils.message("1104", "头信息缺失", e.getMessage());
	}

	@ExceptionHandler(value = MissingServletRequestPartException.class)
	public String MissingServletRequestPartExceptionHandler(MissingServletRequestPartException e) {
		LOG.error("-------MissingServletRequestPartException--------",e);
		return Utils.message("1105", "请上传图片", e.getMessage());
	}

	@ExceptionHandler(value = Exception.class)
	public String errorHandler(Exception e) {
		LOG.error("-------Exception--------",e);
		return Utils.message("1100", "哎呀,出错了", e.getMessage());
	}
}
